package com.googlecode.jlogalizer.mapred.log.reduce;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
;

public class LogReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
	
	@Override
	public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> collector, Reporter reporter) throws IOException {
		
		int sum=0;
		
		for (Iterator iterator = values; iterator.hasNext();) {
			IntWritable value = (IntWritable) iterator.next();
			sum+=value.get();
		}
		
		collector.collect(key, new IntWritable(sum));
		reporter.incrCounter("Key", key.toString(), 1);
	}

}
